﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Congducsan
{
    public partial class FrmTracngang : Form
    {
        public FrmTracngang()
        {
            InitializeComponent();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmTracngang_FormClosing(object sender, FormClosingEventArgs e)
        {
            DradApplication.DradApp.FormMain.Show();
        }

        private void FrmTracngang_Resize(object sender, EventArgs e)
        {
            this.Size = new Size(521, 489);
        }

        private void FrmTracngang_Load(object sender, EventArgs e)
        {
            loadSettingReg();
        }
           // Lưu settings vào registry
        private void saveSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            string Values = "";
            //Cap nhat taluy nen duong
            Values = txtCaochankhaytaluy.Text;//0
            Values = Values +"\t"+txtDaytaluy.Text;//1
            Values = Values +"\t"+txtRongchankhaytaluy1.Text;//2
            Values = Values +"\t"+txtRongchankhaytaluy2.Text;//3
            Values = Values +"\t"+txtRongvaitaluy.Text;//4
            Values = Values + "\t" + Cht1.Checked.ToString() ;//5
            Values = Values +"\t"+Cht2.Checked.ToString();//6
            Values = Values +"\t"+Cht3.Checked.ToString();//7
            Values = Values +"\t"+TxtCaoDoTk.Text;//8
            Values = Values +"\t"+TxtRongLeP.Text;//9
            Values = Values +"\t"+TxtRongLeT.Text;//10
            Values = Values +"\t"+TxtDocLeP.Text;//11
            Values = Values +"\t"+TxtDocLeT.Text;//12
            Values = Values +"\t"+TxtDocNenP.Text;//13
            Values = Values +"\t"+TxtDocNenT.Text;//14
            Values = Values +"\t"+txtHuongduoi.Text;//15
            Values = Values +"\t"+txtHuongtren.Text;//16
            Values = Values +"\t"+txtGocxien.Text;//17
            Values = Values +"\t"+txtVlTl.Text;//18
            Values = Values +"\t"+txtVlCk.Text;//19
            Values = Values +"\t"+txtCaogc.Text;//20
            regKey.SetValue("ktTracNgang", Values);
            //Cap nhat duong tu nhien
            regKey.SetValue("DuongTn", TxtDuongTn.Text.Trim().Replace('\n', '\t'));
            //Cap nhat duong thiet ke
            regKey.SetValue("DuongTk", TxtDuongTk.Text.Trim().Replace('\n', '\t'));
            regKey.Close();

        }
           // Load settings từ registry vào form
        private void loadSettingReg()
        {
            RegistryKey regKey = Registry.CurrentUser;
            RegistryKey testKey = regKey.OpenSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
            if (testKey != null)
            {
                regKey = regKey.CreateSubKey("Software\\HaithanhSoft\\Drad2012\\Congducsan");
                string str = regKey.GetValue("ktTracNgang").ToString();
                string[] STR = str.Split('\t');
                int i = 0;
                //Cap nhat taluy nen duong
                txtCaochankhaytaluy.Text=STR[i++];//0
                txtDaytaluy.Text = STR[i++];//1
                txtRongchankhaytaluy1.Text = STR[i++];//2
                txtRongchankhaytaluy2.Text = STR[i++];//3
                txtRongvaitaluy.Text = STR[i++];//4
                Cht1.Checked = bool.Parse(STR[i++]);//5
                Cht2.Checked = bool.Parse(STR[i++]);//6
                Cht3.Checked = bool.Parse(STR[i++]);//7
                TxtCaoDoTk.Text = STR[i++];//8
                TxtRongLeP.Text = STR[i++];//9
                TxtRongLeT.Text = STR[i++];//10
                TxtDocLeP.Text = STR[i++];//11
                TxtDocLeT.Text = STR[i++];//12
                TxtDocNenP.Text = STR[i++];//13
                TxtDocNenT.Text = STR[i++];//14
                txtHuongduoi.Text = STR[i++];//15
                txtHuongtren.Text = STR[i++];//16
                txtGocxien.Text = STR[i++];//17
                txtVlTl.Text = STR[i++];//18
                txtVlCk.Text = STR[i++];//19
                txtCaogc.Text = STR[i++];//20
                string[] strDuongTn = regKey.GetValue("DuongTn").ToString().Trim().Split('\t');
                for (int j = 0; j < strDuongTn.Length; j++)
                {
                    if (j%2==0)
                    {
                        TxtDuongTn.Text += strDuongTn[j] + "\t";
                    }
                    else
                    {
                        TxtDuongTn.Text += strDuongTn[j] + "\n";
                    }
                }
                string[] strDuongTk = regKey.GetValue("DuongTk").ToString().Trim().Split('\t');
                for (int j = 0; j < strDuongTk.Length; j++)
                {
                    if (j % 2 == 0)
                    {
                        TxtDuongTk.Text += strDuongTk[j] + "\t";
                    }
                    else
                    {
                        TxtDuongTk.Text += strDuongTk[j] + "\n";
                    }
                }
                regKey.Close();
            }
        }

        private void cmdThietlap_Click(object sender, EventArgs e)
        {
            FrmTuychon frmTuychon = new FrmTuychon();
            frmTuychon.Show();
        }

        private void FrmTracngang_FormClosed(object sender, FormClosedEventArgs e)
        {
            saveSettingReg();
        }

        private void TxtDuongTn_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 32)
            {
                e.KeyChar = '\t';
            }
        }

        private void TxtDuongTk_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 32)
            {
                e.KeyChar = '\t';
            }

        }
    }
}
